package practice;

import java.io.IOException;
import java.util.Date;
import java.util.Random;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Logger;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.urlfetch.HTTPResponse;

@SuppressWarnings("serial")
public class JavaPerfTestServlet extends HttpServlet {

	static AtomicInteger counter = new AtomicInteger(0);
	static int count = 0;
	static Random random = new Random();

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {

		StringBuilder builder = new StringBuilder();
		StopWatch sw = StopWatch.createStarted();
		int value = counter.incrementAndGet();

		builder.append("time to increment an atomic integer: "
				+ sw.nanoSecondsElapsed() + " nano seconds. <br>");
		builder.append("value: " + value + "<br>");

		sw = StopWatch.createStarted();
		count++;
		builder.append("time to increment an int: " + sw.nanoSecondsElapsed()
				+ " nano seconds. <br>");

		sw = StopWatch.createStarted();
		int r = random.nextInt(10);
		builder.append("time to generate random no [" + r + "]: "
				+ sw.nanoSecondsElapsed() + " nano seconds. <br>");

		sw = StopWatch.createStarted();
		final StackTraceElement[] ste = Thread.currentThread().getStackTrace();
		builder.append("stack trace obtained in: " + sw.nanoSecondsElapsed()
				+ " nano seconds. <br>");

		sw = StopWatch.createStarted();
		Date date = new Date();
		long seconds = date.getTime();
		builder.append("current date [" + date + "] and time in seconds ["
				+ seconds + "] obtained in: " + sw.nanoSecondsElapsed()
				+ " nano seconds. <br>");

		SynchronizationTest synchronizationTest = new SynchronizationTest();

		sw = StopWatch.createStarted();
		synchronizationTest.dummySynchronized();
		builder.append("time to call a synchronized dummy method: "
				+ sw.nanoSecondsElapsed() + " nanoseconds. <br>");

		sw = StopWatch.createStarted();
		synchronizationTest.dummy();
		builder.append("time to call a non synchronized dummy method: "
				+ sw.nanoSecondsElapsed() + " nanoseconds. <br>");

		sw = StopWatch.createStarted();
		String s = HttpRequest.getAllGroups();
		builder.append("Time to make a http request to appengine app (without setting doNotFollowRedirect): "
				+ sw.milliSecondsElapsed() + " milliseconds. <br>");
		Logger logger = Logger.getLogger(JavaPerfTestServlet.class.getName());
		logger.info("response from another app engine: " + s);

		sw = StopWatch.createStarted();
		Future<HTTPResponse> response = HttpRequest.getAllGroupsAsync();
		builder.append("Time to initiate a http async request to appengine app: "
				+ sw.nanoSecondsElapsed() + " nanoseconds. <br>");

		try {
			response.get();
			builder.append("async url fetch to appengine app completed in: "
					+ sw.milliSecondsElapsed() + " milliseconds. <br>");
			logger.info("response code after url fetch: "
					+ response.get().getResponseCode());

		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		sw = StopWatch.createStarted();
		Random random = new Random();
		int i = random.nextInt(999999);
		builder.append("Time to generate random no [" + i
				+ "] between 0 to 1 million: " + sw.nanoSecondsElapsed()
				+ " nanoseconds. <br>");

		resp.setContentType("text/html");
		resp.getWriter().println(builder.toString());
	}
}
